//
//  main.m
//  C04_作业
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>

#define SIZE 10
int main(int argc, const char * argv[]) {
#if 0
    {
        /** 先调试,调试成功后抄写下列程序 */
        int a[SIZE] = {0}, i = 0;
        for(i = 0; i < SIZE; i++)
        {
            a[i] = i + 10;
            printf("a[%d] = %d\n", i, a[i]);
        }
    }
#endif
    /** 1.将第一题中的数组a反向输出 */
#if 0
    {
        int a[SIZE] = {0}, i = 0;
        for(i = 0; i < SIZE; i++)
        {
            a[i] = i + 10;
            printf("a[%d] = %d\n", i, a[i]);
        }
        for (int i = SIZE - 1; i >= 0; i--) {
            a[i] = SIZE + i;
            printf("%d\n", a[i]);
        }
        
        
    
    }
#endif
    /** 2.对第一题中的数组进行求和操作,打印计算结果 */
#if 0
    {
        int sum = 0;
        int a[SIZE] = {0}, i = 0;
        for(i = 0; i < SIZE; i++)
        {
            a[i] = i + 10;
            printf("a[%d] = %d\n", i, a[i]);
        }
        for (int i = 0; i < SIZE; i++) {
            sum = sum + a[i];
        }
        printf("%d\n", sum);
    
    }
#endif
    /** 3.计算第一题数组连减,打印计算结果 */
#if 0
    {
        int cha = 20;
        int a[SIZE] = {0}, i = 0;
        for(i = 0; i < SIZE; i++)
        {
            a[i] = i + 10;
            printf("a[%d] = %d\n", i, a[i]);
        }
        for (int i = 0; i < SIZE; i++) {
            cha = cha - a[i];
        }
        printf("连减结果为:%d\n", cha);
  
    }
#endif
    /** 4.随机产生20个10~50的正整数存放到数组中,并求数组中的 最大值,最小值,平均值及各个元素之和 */
#if 0
    {
        int array[20] = {0};
        int max = 0;
        int min = 50;
        int sum = 0;
        for (int i = 0; i < 20; i++) {
            array[i] = arc4random() % (50 - 10 + 1) + 10;
        }
        for (int i = 0; i < 20; i++) {
            if (max < array[i]) {
                max = array[i];
            }
            if (min > array[i]) {
                min = array[i];
            }
            sum = sum + array[i];
        }
        
        printf("最大值为:%d\n", max);
        printf("最小值为:%d\n", min);
        printf("各元素之和为:%d\n", sum);
        printf("平均值为:%d\n", sum / 20);
     
    }
#endif
    /** 5.编写一个程序,输入两个包含5个元素的数组,先将两个数组 升序排列,然后将这两个数组合并成一个升序数组 */
#if 0
    {
        int array1[5] = {3, 2, 1, 4, 9};
        int array2[5] = {6, 7, 10, 5, 8};
        int array3[10] = {0};
        int temp = 0;
        for (int i = 0; i < 5 - 1; i++) {
            for (int j = 0; j < 5 - i - 1; j++) {
                if (array1[j] > array1[j + 1]) {
                    temp = array1[j];
                    array1[j] = array1[j + 1];
                    array1[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < 5; i++) {
            printf("%d ", array1[i]);
        }
        printf("\n");
        for (int i = 0; i < 5 - 1; i++) {
            for (int j = 0; j < 5 - i - 1; j++) {
                if (array2[j] > array2[j + 1]) {
                    temp = array2[j];
                    array2[j] = array2[j + 1];
                    array2[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < 5; i++) {
            printf("%d ", array2[i]);
        }
        printf("\n");
        for (int i = 0; i < 10; i++) {
            if (i < 5) {
                array3[i] = array1[i];
            } else {
                array3[i] = array2[i - 5];
            }
            printf("%d ", array3[i]);
        }
        printf("\n");
        for (int i = 0; i < 10 - 1; i++) {
            for (int j = 0; j < 10 - i - 1; j++) {
                if (array3[j] > array3[j + 1]) {
                    temp = array3[j];
                    array3[j] = array3[j + 1];
                    array3[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < 10; i++) {
            printf("%d ", array3[i]);
        }
        printf("\n");
    }
#endif
    /** 6.给定某年某月某日,输出其为这一年的第几天 */
#if 0
    {
        int year;
        int month;
        int day;
        int sum = 0;
        printf("请输入年/月/日:");
        scanf("%d%d%d", &year, &month, &day);
        int a[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (year % 400 == 0 || (year % 4 ==0 && year % 100 != 0)) {
            a[1] = 29;
        }
        for (int i = 1; i < month; i++) {
            sum = sum + a[i];
        }
        printf("是这一年的第%d天\n", sum + day);
    }
#endif
    /** 7.编写整型数组排序程序(冒泡排序-升序) */
#if 0
    {}
#endif
    /** 8.找出下列整型数组中的最大和最小值及其所在位置的下标i
     * int a[] = {5, -9, 32, 77, 64, -24, 14, 0, 21, 45};
     */
#if 0
    {
        int a[] = {5, -9, 32, 77, 64, -24, 14, 0, 21, 45};
        int max = 0;
        int min = 0;
        int index1 = 0;
        int index2 = 0;
        int count = sizeof(a) / sizeof(a[0]);
        for (int i = 0; i < count; i++) {
            if (max < a[i]) {
                max = a[i];
                index1++;
            }
            if (min > a[i]) {
                min = a[i];
                index2++;
            }
        }
        printf("最大值为:%d 下标为:%d\n", max, index1);
        printf("最小值为:%d 下标为:%d\n", min, index2);
    }
#endif
    /** 9.把 str1, str2, str3 合并到 result 数组中。
     
     char result[50] = {0};
     char str1[] = "Lanou ";
     char str2[] = "23_class "; char str3[] = " is niu best!";
     
     结果:“Lanou 23_class is niu best!”. 
     */
#if 0
    {
        char result[50] = {0};
        char str1[] = "Lanou ";
        char str2[] = "23_class ";
        char str3[] = " is niu best!";
        strcpy(result, str1);
        strcat(result, str2);
        strcat(result, str3);
        printf("%s\n", result);
    }
#endif
    /** 10.倒转字符串"abcdef" */
#if 0
    {
        char string[] = "abcdef";
        int count = sizeof(string) / sizeof(string[0]);
        int temp = 0;
        int flag = 1;   // 表示无序
        for (int i = 0; i < count - 1 && 1 == flag; i++) {
            flag = 0;    // 表示有序,假设有序
            for (int j = 0; j < count - i - 1; j++) {
                if (string[j] < string[j + 1]) {
                    temp = string[j];
                    string[j] = string[j + 1];
                    string[j + 1] = temp;
                    flag = 1;     // 发生交换 说明是无序,将flag赋值为1
                }
            }
        }
        printf("%s ", string);
        printf("\n");
    }
#endif
    /** 11.将字符串中的数字去掉  "a12b3c45d78" */
#if 0
    {
        char string[] = "a12b3c45d78";
        int count = sizeof(string) / sizeof(string[0]);
        for (int i = 0; i < count; i++) {
            if (string[i] >= 'a' && string[i] <= 'z') {
                printf("%c", string[i]);
            }
        }
        printf("\n");
   
    }
#endif
    /** 12.编程在一个已知的字符串中找最长单词，假定字符串中只含字母和空格，空格用来分隔不同单词。
     比如："ni hao world",最长单词是world */
#if 0
    {
        char string[] = "ni hao world";
        int count = sizeof(string) / sizeof(string[0]);
        for (int i = 0; i < count; i++) {
            if (string[i] == ' ') {
                break;
            }
            printf("%c", string[i]);
        }
        
    }
#endif
    
    
    
    
    return 0;
}
